#include "lcd.h"
#include "main.h"
#include "lvgl.h"
#include "lcd_lvgl_config.h"


void lvgl_flush_cb(lv_disp_drv_t * disp_drv, const lv_area_t * area, lv_color_t * color_p)
{	
	LCD_Fill_Colors(area->x1,area->y1,area->x2,area->y2,color_p);

    // int32_t x, y;
    // for(y = area->y1; y <= area->y2; y++) {
    //     for(x = area->x1; x <= area->x2; x++) {

	// 	   LCD_DrawPoint(x, y,  (color_p->full));

    //         color_p++;
    //     }
    // }


    lv_disp_flush_ready(disp_drv);
}

int mouse_x;
int mouse_y;
int mouse_sta;

bool lvgl_mouse_read(lv_indev_drv_t * indev_drv, lv_indev_data_t * data)
{
    (void) indev_drv;   

    data->point.x = mouse_x;
    data->point.y = mouse_y;
    data->state = mouse_sta ? LV_INDEV_STATE_PR : LV_INDEV_STATE_REL;
    return false;
}

static lv_disp_buf_t LVGL_disp_buf;
static lv_color_t LVGL_buf1[LV_HOR_RES_MAX * 10];    
static lv_color_t LVGL_buf2[LV_HOR_RES_MAX * 10];  

uint8_t lcd_lvgl_init(void)
{
    esp_err_t ret;


	lv_disp_buf_init(&LVGL_disp_buf, LVGL_buf1, LVGL_buf2, LV_HOR_RES_MAX * 10);  

    lv_disp_drv_t disp_drv;      
    lv_disp_drv_init(&disp_drv);  
	disp_drv.buffer=&LVGL_disp_buf;
	disp_drv.flush_cb = lvgl_flush_cb;
    lv_disp_drv_register(&disp_drv);


    lv_indev_drv_t indev_drv;
    lv_indev_drv_init(&indev_drv);          
    indev_drv.type = LV_INDEV_TYPE_POINTER;
    indev_drv.read_cb = lvgl_mouse_read;     
    lv_indev_drv_register(&indev_drv);




 	return 0;
}



